3. Unaware of:this “this” is not the “this” function test(name, button) { this.name = name; button.onclick= function(event) { alert('hello, ' + this.name); }; }
4. Unaware of:“not of not” is not the not of “not” function test(foo) { alert(typeof(foo)); alert(foo); if (foo) { alert('foo'); } if (!foo) { alert(‘!foo'); } if (!!foo) { alert('!!foo'); } }
5. Misunderstaning0, null, false, undefined, NaN and “” function test(foo) { alert(typeof(foo)); alert(foo); if (foo) { alert('foo'); } if (foo == null) { alert('foo == null'); } if (foo === null) { alert('foo === null'); } }
6. Trying todeclaring block scope “var”s function test() { var foo = -1; for (varfoo = 0; foo < 10; foo++) { … } alert(foo); }
7. Trying tooverload a function function test(foo) { alert('one arg'); } function test(foo, bar) { alert('two args'); } function test() { for (var i = 0; i < arguments.length; i++) { alert(arguments[i]); } }
8. Using escape() instead of encodeURIComponent(), encodeURI() Checkout http://xkr.us/articles/javascript/encode-compare/ Rule of thumb! use encodeURIComponent() use encodeURI() only if you know what it is forget escape()
9. Unaware ofstring is not array-of-characters var str = “hello”; alert(str.charAt(2)); alert(str[2]); Similarly, NodeList is not array-of-Nodestoo!
10. Missing“radix” for parseInt() var sum = parseInt(num1.value) + parseInt(num2.value); alert(sum); parseInt('1234'); parseInt('01234'); parseInt('1234', 8); parseInt('01234', 10);
11. Sorry…it’s a comma… just a trailing comma… var test = { 1, 2, 3, }; out.print(“{“); for (int val: values) { out.print(i); out.print(“,”); } out.print(“}”);
12. Using Vanilla JavaScript Checkout http://en.wikipedia.org/wiki/Comparison_of_JavaScript_frameworks http://jquery.com/ http://prototypejs.org/ http://dojotoolkit.org/ and so on…
13. Testing in one browserand expecting it to work in another Checkout http://www.quirksmode.org/